﻿using System;

namespace MVCFoto.Web.Utility
{
    public class Cache
    {
        /// <summary>
        /// 清除应用程序或网站所有缓存，包括页面缓存 
        /// </summary>
        public static void ClearAllCache()
        {
            System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache;
            if (c != null)
            {
                System.Collections.IDictionaryEnumerator cacheEnumerator = c.GetEnumerator();
                while (cacheEnumerator.MoveNext())
                {
                    c.Remove(cacheEnumerator.Key.ToString());
                }
            }
        }

        /// <summary>
        /// 清除对应key的缓存项
        /// </summary>
        /// <param name="Key">Key</param>
        public static void ClearCacheItem(string Key)
        {
            if (System.Web.HttpRuntime.Cache[Key] != null)
            {
                System.Web.HttpRuntime.Cache.Remove(Key);
                System.Web.HttpContext.Current.Trace.Warn("Cache", "Item " + Key + " removed from cache");
            }
        }
    }
}
